import tkinter as tk
from pml import Objects
from pml import PML

class Terminal(Objects.object.typeobject):
    def __init__(self):
        super().__init__('Terminal')
        self.root = tk.Tk()
        self.root.title('PML Notepad Terminal')
        self.root.geometry('800x500')

    def getObject(self):
        return self

    @staticmethod
    def write(frame, this, position, text):
        label = tk.Label(this.root, font='仿宋', anchor='w', width=800)
        label['text'] = text if isinstance(text, str) else text.__repr__()
        label.pack()
        label.update()
        this.root.update()

    @staticmethod
    def get(frame, this, position, text):
        label = tk.Label(this.root, font='仿宋', anchor='w')
        label['text'] = text if isinstance(text, str) else text.__repr__()
        label.pack()
        label.update()
        entry = tk.Entry(this.root)
        entry.insert(0, '')
        entry.pack()
        t = None
        def getValue():
            nonlocal t
            t = entry.get()
        entry.bind('<Return>', getValue)
        while t is None:
            entry.update()
            this.root.update()
        return PML.object.ChangeObject.PMLString_FromStr(t)

    def run(self):
        self.root.mainloop()
